using System;
using System.Collections;
using System.Collections.Generic;
using Holoville.HOTween;
using I2.Loc;
using UnityEngine;

public class SearchEnemies : MonoBehaviour
{
	public static SearchEnemies instance;

	public bool enableArrow;

	public GameObject arrowOnMob;

	public GameObject trackingObj;

	public Vector3 trackingPosition;

	public SectorMobManager trackingSector;

	public int numberSkinTrack;

	public List<ArrowMob> listTrackingObj = new List<ArrowMob>();

	public List<SectorMobManager> listTrackingSectors = new List<SectorMobManager>();

	public List<MobGroup> listNeedGroup = new List<MobGroup>();

	private EnemyBehavior findEnemy;

	public float msgPromptShowTime = 10f;

	public int msgPromptMaxShow = 3;

	public int msgPromptCount;

	private string ppPromptKey = "arrow_prompt_count";

	private GameObject _OldTrackingObj;

	private SectorMobManager _OldTrackingSector;

	private PointerPanel pointer;

	private void Awake()
	{
		instance = this;
		trackingPosition = Vector3.zero;
		HOTween.Init();
	}

	private void Start()
	{
		ManagerPreloadingSectors thisScript = ManagerPreloadingSectors.thisScript;
		thisScript.GameStartedHandler = (Action)Delegate.Combine(thisScript.GameStartedHandler, new Action(CheckAndShowPrompt));
		pointer = PointerController.instance.AddPointer(trackingPosition, 0f, "iconMap_Red Cercle");
		if ((bool)pointer)
		{
			pointer.maxIconPosCorrection = 150f;
		}
		GameModeChange();
		settings.gameModeChange += GameModeChange;
	}

	private void GameModeChange()
	{
		base.enabled = settings.offlineMode;
		if (!base.enabled && (bool)pointer)
		{
			pointer.gameObject.SetActive(false);
		}
	}

	private void OnDestroy()
	{
		settings.gameModeChange -= GameModeChange;
	}

	private void LateUpdate()
	{
		if (enableArrow && GameController.instance.myPlayer != null)
		{
			if (Time.frameCount % 10 == 0)
			{
				float num = 100000f;
				float num2 = num;
				if (listTrackingObj.Count == 0 || (trackingObj != null && !trackingObj.activeSelf))
				{
					trackingObj = null;
				}
				if (trackingObj == null || Vector3.Distance(trackingObj.transform.position, GameController.instance.myPlayer.transform.position) > 10f)
				{
					findEnemy = null;
					for (int i = 0; i < listTrackingObj.Count; i++)
					{
						if (listTrackingObj[i].objTrack.gameObject.activeSelf)
						{
							num2 = Vector3.Distance(GameController.instance.myPlayer.transform.position, listTrackingObj[i].objTrack.transform.position);
							if (num2 < num)
							{
								findEnemy = listTrackingObj[i].objTrack;
								num = num2;
							}
						}
					}
				}
				if (findEnemy == null)
				{
					trackingSector = GetNearSector();
					trackingObj = null;
					if (trackingSector == null)
					{
						trackingPosition = Vector3.zero;
					}
					else
					{
						trackingPosition = trackingSector.centerPoint;
					}
					if (Vector3.Distance(trackingPosition, GameController.instance.myPlayer.transform.position) <= 70f)
					{
						MobGroup needGroup = null;
						if (listNeedGroup.Count > 0)
						{
							needGroup = listNeedGroup[UnityEngine.Random.Range(0, listNeedGroup.Count)];
						}
						MobSpawnController.instance.ForceSpawnSingleMob(needGroup, trackingSector);
					}
				}
				else
				{
					trackingObj = findEnemy.gameObject;
				}
				UpdateIconMap();
			}
			if (trackingObj != null || trackingPosition != Vector3.zero)
			{
				if (trackingObj != null)
				{
					trackingPosition = trackingObj.transform.position;
				}
				if ((bool)pointer)
				{
					pointer.gameObject.SetActive(true);
					pointer.SetNewTargetVector(trackingPosition);
				}
			}
			else if ((bool)pointer)
			{
				pointer.gameObject.SetActive(false);
			}
		}
		else if ((bool)pointer)
		{
			pointer.gameObject.SetActive(false);
		}
	}

	private void UpdateIconMap()
	{
		for (int i = 0; i < listTrackingSectors.Count; i++)
		{
			if (listTrackingSectors[i] == trackingSector)
			{
				listTrackingSectors[i].IsActiveIconMap = true;
			}
			else
			{
				listTrackingSectors[i].IsActiveIconMap = false;
			}
		}
	}

	public void AddTargetForTracking(GameObject obj)
	{
		bool flag = false;
		for (int i = 0; i < listTrackingObj.Count; i++)
		{
			if (listTrackingObj[i].objTrack.gameObject.Equals(obj))
			{
				flag = true;
				break;
			}
		}
		if (!flag)
		{
			ArrowMob item = default(ArrowMob);
			item.objTrack = obj.GetComponent<EnemyBehavior>();
			item.objArrow = UnityEngine.Object.Instantiate(arrowOnMob);
			if (item.objTrack != null && item.objTrack.iconMiniMap != null)
			{
				item.objTrack.iconMiniMap.SetType("MissionTarget");
				item.objTrack.iconMiniMap.enabled = true;
			}
			item.SetPosArrow();
			listTrackingObj.Add(item);
		}
	}

	public void RemoveTargetForTracking(GameObject obj)
	{
		int num = -1;
		for (int i = 0; i < listTrackingObj.Count; i++)
		{
			if (listTrackingObj[i].objTrack.gameObject.Equals(obj))
			{
				num = i;
				break;
			}
		}
		if (num >= 0)
		{
			UnityEngine.Object.Destroy(listTrackingObj[num].objArrow);
			listTrackingObj.RemoveAt(num);
		}
	}

	public void AddSectorsForNumberSkin(int numVal)
	{
		bool flag = false;
		foreach (MobGroup mobGroup in MobTypesManager.instance.mobGroups)
		{
			flag = false;
			foreach (MobGroupParms enemy in mobGroup.enemies)
			{
				foreach (MobSkin skin in enemy.mob.skinList)
				{
					if (skin.skinNumbers == numVal)
					{
						flag = true;
						break;
					}
				}
				if (flag)
				{
					break;
				}
			}
			if (flag && !listNeedGroup.Contains(mobGroup))
			{
				listNeedGroup.Add(mobGroup);
			}
		}
		bool flag2 = false;
		SectorMobManager[] allMobsSectors = MobSpawnController.instance.allMobsSectors;
		foreach (SectorMobManager sectorMobManager in allMobsSectors)
		{
			flag2 = false;
			foreach (MobSectorGroupParams mobGroup2 in sectorMobManager.mobGroups)
			{
				foreach (MobGroup item in listNeedGroup)
				{
					if (mobGroup2.group.gameObject.name == item.gameObject.name)
					{
						flag2 = true;
						break;
					}
				}
				if (flag2)
				{
					break;
				}
			}
			if (flag2 && !listTrackingSectors.Contains(sectorMobManager))
			{
				listTrackingSectors.Add(sectorMobManager);
			}
		}
	}

	public void AddNeededSectorsWeithSkin(List<SectorMobManager> neededMobSectors, int neededMobSkin)
	{
		bool flag = false;
		foreach (MobGroup mobGroup in MobTypesManager.instance.mobGroups)
		{
			flag = false;
			foreach (MobGroupParms enemy in mobGroup.enemies)
			{
				foreach (MobSkin skin in enemy.mob.skinList)
				{
					if (skin.skinNumbers == neededMobSkin)
					{
						flag = true;
						break;
					}
				}
				if (flag)
				{
					break;
				}
			}
			if (flag && !listNeedGroup.Contains(mobGroup))
			{
				listNeedGroup.Add(mobGroup);
			}
		}
		bool flag2 = false;
		foreach (SectorMobManager neededMobSector in neededMobSectors)
		{
			flag2 = false;
			foreach (MobSectorGroupParams mobGroup2 in neededMobSector.mobGroups)
			{
				foreach (MobGroup item in listNeedGroup)
				{
					if (mobGroup2.group.gameObject.name == item.gameObject.name)
					{
						flag2 = true;
						break;
					}
				}
				if (flag2)
				{
					break;
				}
			}
			if (flag2 && !listTrackingSectors.Contains(neededMobSector))
			{
				listTrackingSectors.Add(neededMobSector);
			}
		}
	}

	public void ClearSectorList()
	{
		if (listTrackingSectors.Count > 0)
		{
			listTrackingSectors.Clear();
		}
		if (listNeedGroup.Count > 0)
		{
			listNeedGroup.Clear();
		}
		if (MobSpawnController.instance != null)
		{
			for (int i = 0; i < MobSpawnController.instance.allMobsSectors.Length; i++)
			{
				MobSpawnController.instance.allMobsSectors[i].IsActiveIconMap = false;
			}
		}
		if (TrackArrowController.instance != null)
		{
			TrackArrowController.instance.DestroyAllTrackParent(base.transform);
		}
	}

	private SectorMobManager GetNearSector()
	{
		float num = float.MaxValue;
		float num2 = num;
		SectorMobManager result = null;
		for (int i = 0; i < listTrackingSectors.Count; i++)
		{
			num2 = Vector3.Distance(GameController.instance.myPlayer.transform.position, listTrackingSectors[i].centerPoint);
			if (num2 < num)
			{
				result = listTrackingSectors[i];
				num = num2;
			}
		}
		return result;
	}

	public void RemoveAllArrow()
	{
		ClearSectorList();
		for (int i = 0; i < listTrackingObj.Count; i++)
		{
			if (listTrackingObj[i].objTrack != null && listTrackingObj[i].objTrack.iconMiniMap != null)
			{
				listTrackingObj[i].objTrack.iconMiniMap.enabled = false;
			}
			UnityEngine.Object.Destroy(listTrackingObj[i].objArrow);
		}
		listTrackingObj.Clear();
	}

	private void CheckAndShowPrompt()
	{
		if (!settings.offlineMode || !enableArrow)
		{
			return;
		}
		if (PlayerPrefs.HasKey(ppPromptKey))
		{
			msgPromptCount = PlayerPrefs.GetInt(ppPromptKey);
			if (msgPromptCount > msgPromptMaxShow)
			{
				return;
			}
		}
		msgPromptCount++;
		PlayerPrefs.SetInt(ppPromptKey, msgPromptCount);
		if (Device.indexWeakDevice <= 0)
		{
			GameTopWindow.instance.lbTextTutorial.text = ScriptLocalization.Get("Game_Common/src_FollowArrow");
			GameTopWindow.instance.lbTextTutorial.gameObject.SetActive(true);
			StartCoroutine("HideMissionArrowPrompt");
		}
	}

	private IEnumerator HideMissionArrowPrompt()
	{
		yield return new WaitForSeconds(msgPromptShowTime);
		GameTopWindow.instance.lbTextTutorial.gameObject.SetActive(false);
	}

	private void OnApplicationQuit()
	{
		ManagerPreloadingSectors thisScript = ManagerPreloadingSectors.thisScript;
		thisScript.GameStartedHandler = (Action)Delegate.Remove(thisScript.GameStartedHandler, new Action(CheckAndShowPrompt));
	}
}
